package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.CommonDict;
import common.Menu;
import entities.TMerchant;
import entities.TSubject;
import interfaces.ISubjectService;
import net.sf.json.JSONArray;
import utils.AesUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.KeyUtil;
import utils.StringUtil;

public class SubjectAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private ISubjectService subjectService;

	private TSubject subjectBean;
	private List<TSubject> subjectList;

	private String subNo;
	private String subNos;
	private String subName;
	private String subPwd;
	private String subKey;
	private String subMenu;
	private String subState;
	private Date subDate;
	private Date subDateTop;
	private Date subDateEnd;
	private String subRemark;

	private String merNo;

	// 展示对象
	public String show() throws Exception {
		return SUCCESS;
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!subjectService.exist(subNo)) {
			subjectBean = new TSubject();
			subjectBean.setSubMenu(CommonDict.SUBJECT_MENU);
		} else {
			subjectBean = subjectService.fetch(subNo);
			subjectBean.setSubPwd(AesUtil.AES_Decrypt(AesUtil.KEY, subjectBean.getSubPwd()));
		}
		return SUCCESS;
	}

	// 编辑对象
	public String editpwd() throws Exception {
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		if (!subjectService.exist(subNo)) {
			subjectBean = new TSubject();
			subjectBean.setSubNo(((TMerchant)  ActionContext.getContext().getSession().get("merchant")).getMerNo());
			subjectBean.setSubKey(KeyUtil.generate(CommonDict.KEY_LENGTH));
		} else {
			subjectBean = subjectService.fetch(subNo);
		}
		subjectBean.setSubName(subName);
		subjectBean.setSubPwd(AesUtil.AES_Encrypt(AesUtil.KEY, subPwd));
		subjectBean.setSubMenu(Menu.build(subMenu));
		subjectBean.setSubState("1");
		subjectBean.setSubDate(new Date());
		subjectBean.setSubRemark(subRemark);
		subjectService.persist(subjectBean);
		return SUCCESS;
	}

	// 持久化对象
	public void savestate() throws Exception {
		if (!subjectService.exist(subNo)) {
			addActionError("数据不存在");
			ServletActionContext.getResponse().getWriter().print("fail");
		} else {
			subjectBean = subjectService.fetch(subNo);
		}
		subjectBean.setSubNo(subNo);
		subjectBean.setSubState(subState);
		subjectService.persist(subjectBean);
		ServletActionContext.getResponse().getWriter().print("succ");
	}

	// 持久化对象
	public String savepwd() throws Exception {
		subjectBean=(TSubject)  ActionContext.getContext().getSession().get("subject");
		subjectBean.setSubPwd(AesUtil.AES_Encrypt(AesUtil.KEY, subPwd));
		subjectService.persist(subjectBean);
		return SUCCESS;
	}

	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("subDefault",ActionContext.getContext().getSession().get("query").toString());

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField("subNo");
		sortInfo.setSortOrder(new Boolean(true));

		querySet.setPageInfo(new PageInfo(pageIndex, Integer.MAX_VALUE));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TSubject> resultSet = subjectService.search(querySet);
		subjectList = resultSet.getResultList();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("subDefault",ActionContext.getContext().getSession().get("query").toString());

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "subDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TSubject> resultSet = subjectService.search(querySet);
		subjectList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("名称");
		fieldHead.add("密钥");
		fieldHead.add("权限菜单");
		fieldHead.add("状态");
		fieldHead.add("日期日期");
		fieldHead.add("日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TSubject subjectBean : subjectList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(subjectBean.getSubNo());
			fieldItem.add(subjectBean.getSubName());
			fieldItem.add(subjectBean.getSubKey());
			fieldItem.add(subjectBean.getSubMenu());
			fieldItem.add((subjectBean.getSubState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(subjectBean.getSubDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(subjectBean.getSubDate(), DateUtil.TIME));
			fieldItem.add(subjectBean.getSubRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public ISubjectService getSubjectService() {
		return subjectService;
	}

	public void setSubjectService(ISubjectService subjectService) {
		this.subjectService = subjectService;
	}

	public TSubject getSubjectBean() {
		return subjectBean;
	}

	public void setSubjectBean(TSubject subjectBean) {
		this.subjectBean = subjectBean;
	}

	public List<TSubject> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(List<TSubject> subjectList) {
		this.subjectList = subjectList;
	}

	public String getSubNo() {
		return subNo;
	}

	public void setSubNo(String subNo) {
		this.subNo = subNo;
	}

	public String getSubNos() {
		return subNos;
	}

	public void setSubNos(String subNos) {
		this.subNos = subNos;
	}

	public String getSubName() {
		return subName;
	}

	public void setSubName(String subName) {
		this.subName = subName;
	}

	public String getSubPwd() {
		return subPwd;
	}

	public void setSubPwd(String subPwd) {
		this.subPwd = subPwd;
	}

	public String getSubKey() {
		return subKey;
	}

	public void setSubKey(String subKey) {
		this.subKey = subKey;
	}

	public String getSubState() {
		return subState;
	}

	public void setSubState(String subState) {
		this.subState = subState;
	}

	public Date getSubDate() {
		return subDate;
	}

	public void setSubDate(Date subDate) {
		this.subDate = subDate;
	}

	public Date getSubDateTop() {
		return subDateTop;
	}

	public void setSubDateTop(Date subDateTop) {
		this.subDateTop = subDateTop;
	}

	public Date getSubDateEnd() {
		return subDateEnd;
	}

	public void setSubDateEnd(Date subDateEnd) {
		this.subDateEnd = subDateEnd;
	}

	public String getSubRemark() {
		return subRemark;
	}

	public void setSubRemark(String subRemark) {
		this.subRemark = subRemark;
	}

	public String getMerNo() {
		return merNo;
	}

	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}

	public String getSubMenu() {
		return subMenu;
	}

	public void setSubMenu(String subMenu) {
		this.subMenu = subMenu;
	}

}
